#pragma once

#include <vector>

#include "exports.hpp"
#include "service.hpp"
#include "task.hpp"




namespace services
{
	/**
	 * This class offers the basic interface as proposed by the new c++0x standard.
	 */
	class SERVICES_API scheduler
		: public service
	{
	public:

		SHARED(scheduler);

		static const QString SERVICE_NAME;

	public:

		scheduler(boost::shared_ptr<const provider>);
		virtual ~scheduler();



		/**
		 * Get the name of the Service.
		 * This should be unique per Service implementation.
		 */
		virtual QString name() const;



		/**
		 * @returns a list of all currently running tasks
		 */
		virtual std::vector<task::const_ptr> activeTasks() = 0;



		/**
		 * Executes the given functor and returns a future that eventually holds
		 * the value returned by that functor (if any) or the exception thrown
		 * by the functor.
		 *
		 * It is unspecified as to how and when the functor is executed: It may happen
		 * on another thread. It is advised to synchronize any access to shared data
		 * that happens within the functor.
		 */
		template <typename R, typename F>
		future<R> execute(F fn)
		{
			future<R> ft;
			auto t = boost::make_shared<task>(fn, ft);
			executeTask(t);

			return ft;
		}

		/**
		 * Executes the given task.
		 * Using this function directly provides the user with a better way to control the way
		 * tasks are executed.
		 *
		 * @param t           the task to execute
		 */
		virtual void executeTask(task::ptr t) = 0;
	};
	///////////////////////////////////////////////////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////////////////////////
